let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
		certificates: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

	new app.ToastPannel();
	
	var that = this
    // 加载保存的用户信息
	that.requests();
  },
  requests:function(){
      var that = this
      var key = wx.getStorageSync('key')
      var uid = wx.getStorageSync('u')
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=resumeDetail&typ=json',
        method: 'GET',
        data: { k: key, u: uid },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.status == 200) {
  			const user_info = res.data.data
  			that.setData({
  				certificates: user_info.certification_info
  			})
          } else {
            that.show('数据加载失败')
          }
    
        }
      })
    },
  // 上传证书
  uploadCertificate() {
  	wx.chooseImage({
  	  count: 1,
  	  sizeType: ['original', 'compressed'],
  	  sourceType: ['album', 'camera'],
  	  success: (res) => {
  	    const tempFilePaths = res.tempFilePaths;
  	    
  	    // 上传图片到服务器
  	    this.uploadFile(tempFilePaths[0]);
  	  }
  	});
  },
  // 上传文件到服务器
   uploadFile(filePath) {
     wx.showLoading({
       title: '上传中...',
     });
     
     wx.uploadFile({
       url: app.configData.shop.api_url + '/index.php?ctl=Upload&action=uploadImage&typ=json',
       filePath: filePath,
       name: 'upfile',
       header: {
           'content-type': 'multipart/form-data'
       },
       formData: {
           'user': 'test'
       },
       success: (res) => {
         wx.hideLoading();
         const data = JSON.parse(res.data);
         if(data.state === "SUCCESS") {
           wx.showToast({
             title: '上传成功',
             icon: 'success'
           });
           // 可以在这里保存服务器返回的文件路径
  			const newFile = {
  				path: data.url
  			}
  			this.setData({ certificates: [...this.data.certificates, newFile] })
         } else {
           wx.showToast({
             title: '上传失败',
             icon: 'none'
           });
         }
       },
       fail: (err) => {
         wx.hideLoading();
         wx.showToast({
           title: '上传失败',
           icon: 'none'
         });
       }
     });
   },
  // 删除证书
  deleteCertificate(e) {
  	const index = e.currentTarget.dataset.index
  	const certs = this.data.certificates
  	certs.splice(index, 1)
  	this.setData({ certificates: certs })
  },
  saveInfo() {
  	const that = this
  	const key = wx.getStorageSync('key')
  	const uid = wx.getStorageSync('u')
	const parme = {
		k: key, 
		u: uid,
		certification_info: JSON.stringify(that.data.certificates)
	};
	wx.request({
		url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=addResumeCertificatesInfo&typ=json',
		data: parme,
		method: 'POST', 
		header: {
			"Content-Type": "application/x-www-form-urlencoded"
		}, // 设置请求的 header
		success: function (res) {
			if(res.data.status == 200){
				app.show('保存成功')
				wx.redirectTo({
					url: '../personal_resume/personal_resume',
				})
			}else{
				app.show('保存失败')
			}
		},
		fail: function (res) {
		  
		}
	})
  }
})